package com.lottery.im.module.service.ui;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import butterknife.BindView;
import butterknife.OnClick;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.lottery.im.R;
import com.lottery.im.api.result.entity.UserInfo;
import com.lottery.im.api.result.entity.integral.Commodity;
import com.lottery.im.base.BaseSwipeFragment;
import com.lottery.im.module.service.adapter.CommodityAdapter;
import com.lottery.im.module.service.presenter.CommodityGetLimitPresenter;
import com.lottery.im.module.service.view.CommodityGetLimitView;
import com.lottery.im.util.SpacesItemDecoration;
import com.lottery.im.view.DancingView;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import java.util.ArrayList;
import java.util.List;

/**
 * 作者：cyy
 * 时间：2018/9/17。
 */
@SuppressLint("ValidFragment") public class CommodityGetLimitFragment
    extends BaseSwipeFragment<CommodityGetLimitPresenter>
    implements CommodityGetLimitView, BaseQuickAdapter.OnItemClickListener {
  @BindView(R.id.tv_integral) DancingView tvIntegral;
  @BindView(R.id.rv) RecyclerView rv;
  @BindView(R.id.srf) SmartRefreshLayout srf;

  private List<Commodity> mList = new ArrayList<>();
  private CommodityAdapter mAdapter;
  private double amount;

  private final static int REQUESTCODE = 100;
  public final static int RESULTCODE = 600;

  @SuppressLint("ValidFragment") public CommodityGetLimitFragment(double amount) {
    this.amount = amount;
  }

  @Override protected int getLayoutResId() {
    return R.layout.fragment_commodity_limit;
  }

  @Override protected void initPresenter() {
    presenter = new CommodityGetLimitPresenter(getActivity(), this, this);
    presenter.init();
  }

  @Override public void initView() {
    mList.add(new Commodity("积分兑换"));
    tvIntegral.setText(String.valueOf(amount));

    rv.setLayoutManager(new GridLayoutManager(mActivity, 2));
    rv.addItemDecoration(new SpacesItemDecoration(10));

    mAdapter = new CommodityAdapter(mList, getActivity());
    rv.setAdapter(mAdapter);
    mAdapter.setOnItemClickListener(this);

    presenter.queryCommodityList("0");
  }

  @Override public void loadCommoditySucces(List<Commodity> list) {
    if (list.size() > 0) {
      list.add(0, new Commodity("积分兑换"));
    } else {
      list.add(new Commodity("积分兑换"));
    }
    mAdapter.setNewData(list);
  }

  @OnClick({ R.id.iv_back, R.id.tv_notes }) public void onViewClicked(View view) {
    switch (view.getId()) {
      case R.id.iv_back:
        popBackStack();
        break;
      case R.id.tv_notes:
        startFragment(new ExchangeLogFragment());
        break;
    }
  }

  @Override public void onItemClick(BaseQuickAdapter baseQuickAdapter, View view, int i) {
    List<Commodity> list = baseQuickAdapter.getData();
    if (i == 0) {
      startFragmentForResult(new CashExchangeFragment(), REQUESTCODE);
    } else {
      startFragment(new CommodityDetailFragment(list.get(i).getId(), amount));
    }
  }

  @Override protected void onFragmentResult(int requestCode, int resultCode, Intent data) {
    super.onFragmentResult(requestCode, resultCode, data);
    if (requestCode == REQUESTCODE && resultCode == CashExchangeFragment.RESULTCODE) {
      presenter.queryUserCommonInfo();
    }
  }

  @Override public void loadUserCommonInfoSuccess(UserInfo userCommonMapBean) {
    tvIntegral.setText(String.valueOf(userCommonMapBean.getAmount()));
    tvIntegral.setDuration(amount =  userCommonMapBean.getAmount());
    tvIntegral.dance();
  }

  @Override public void onStop() {
    super.onStop();
    setFragmentResult(RESULTCODE, null);
  }
}
